import { showSection, formatFileSize } from './utils.js';

// 加载文档列表
export async function loadDocuments() {
    try {
        const response = await fetch('/api/documents', {
            headers: {
                'Authorization': `Bearer ${localStorage.getItem('access_token')}`
            }
        });

        if (response.ok) {
            const documents = await response.json();
            const tableBody = document.getElementById('docs-table-body');
            tableBody.innerHTML = '';

            documents.forEach(doc => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${doc.id}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${doc.file_name}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${doc.file_type}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${formatFileSize(doc.file_size)}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${new Date(doc.created_at).toLocaleString()}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                        <button onclick="viewDocument('${doc.id}')" class="text-blue-600 hover:text-blue-900 mr-3">查看</button>
                        <button onclick="deleteDocument('${doc.id}')" class="text-red-600 hover:text-red-900">删除</button>
                    </td>
                `;
                tableBody.appendChild(row);
            });
        }
    } catch (error) {
        console.error('加载文档列表错误:', error);
    }
}

// 初始化文档管理
export function initDocuments() {
    loadDocuments();

    // 上传文档按钮事件
    document.getElementById('upload-doc-btn').addEventListener('click', showUploadModal);

    // 上传提交按钮事件
    document.getElementById('upload-submit-btn').addEventListener('click', uploadDocument);

    // 取消上传按钮事件
    document.getElementById('cancel-upload-btn').addEventListener('click', hideUploadModal);
}

// 其他文档相关函数...